var app = getApp(); 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    logourl: '/image/logo.png',
    title:'',
    content: '',
    message:'',
    jsonresult:'',
    typearray: [],
    typeId: 0,
    userarray: [],
    userId: '',
    user:{},
  },

  queInput: function (e){
      this.setData({
        title: e.detail.value
      })
  },

  detailInput: function (e) {
    this.setData({
      content: e.detail.value
    })
  },

  inputfocus: function (e) {
    this.setData({
      message: ""
    })
  },
  
  onLoad: function () {
    console.log('onLoad')
    this.setData({
      user: app.globalData.user
    })
    //调用应用实例的方法获取全局数据
    this.getAllTypes();
    this.getAllUsers();
  },

  getAllTypes:function(){
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/type_getAllTypes.action',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            typearray: jsonresult.types,
          })
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })
  },

  getAllUsers: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/user_getAllUserExceptSelf.action',
      data: {
        "user.userId": this.data.user.userId
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            userarray: jsonresult.users,
          })
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })
  },

  submit:function(e){
    if (this.data.title == ""){
      this.setData({
        message:"请完整填写信息"
      })
    } else if (this.data.userId != ""){
      var that = this
      wx.request({
        url: 'https://eis.natappvip.cc/ITSupport/topic_addTopic.action', 
        data: {
          'topic.title': this.data.title,
          'topic.content': this.data.content,
          'topic.userByToUserId.userId': this.data.userarray[this.data.userId].userId,
          'topic.type.typeId': this.data.typearray[this.data.typeId].typeId,
          'topic.userByUserId.userId': this.data.user.userId,
        },
        header: { 
          "content-type": "application/x-www-form-urlencoded"
        }, 
        method: "POST",
        success: function (res) {
          let jsonresult =JSON.parse(res.data)
          console.log(jsonresult.result)
          if (jsonresult.result == "error"){
             that.setData({
               message: jsonresult.msg 
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '发布成功,是否返回问题列表？',
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })
                } else {

                }
              }
            })
          }
        },
        fail: function (err) {
          console.log(err)
        }
      })
    }else{
      var that = this
      wx.request({
        url: 'https://eis.natappvip.cc/ITSupport/topic_addTopic.action',
        data: {
          'topic.title': this.data.title,
          'topic.content': this.data.content,
          'topic.type.typeId': this.data.typearray[this.data.typeId].typeId,
          'topic.userByUserId.userId': this.data.user.userId,
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        success: function (res) {
          let jsonresult = JSON.parse(res.data)
          console.log(jsonresult.result)
          if (jsonresult.result == "error") {
            that.setData({
              message: jsonresult.msg
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '发布成功,是否返回问题列表？',
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })
                } else {

                }
              }
            })
          }
        },
        fail: function (err) {
          console.log(err)
        }
      })
    }
  },
  bindUserChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      userId: e.detail.value,
    })
  },
  bindTypeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      typeId: e.detail.value,
    })
  },
})